// import {
//     defaultEquals
// } from "./util";
// import {
//     Node
// } from "./models/linked-list-models";

export class Node {
    constructor(element) {
        this.element = element;
        this.next = null;
    }
}
export function defaultEquals(a, b) {
    return a === b;
}

export default class LinkedList {
    constructor(equalsFn = defaultEquals) {
        this.count = 0;
        this.head = null;
        this.equalsFn = equalsFn;
    }
    push(element) {
        const node = new Node(element);
        if (this.head === null) {
            this.head = node;
        } else {
            let curNode = this.head;
            while (curNode.next !== null) {
                curNode = curNode.next;
            }
            curNode.next = node;
        }
        this.count++;
    }
    removeAt(index) {
        if (index >= 0 && index <= this.count) {
            let curNode = this.head;
            if (index === 0) {
                this.head = curNode.next;
            } else {
                // let previous;
                // for (let i = 0; i < index; i++) {
                //     previous = curNode;
                //     curNode = curNode.next;
                // }
                // previous.next = curNode.next;
                const previous = this.getElementAt(index - 1);
                curNode = previous.next;
                previous.next = curNode.next;
            }
            this.count--;
            return curNode.element;
        }
        return undefined;
    }
    getElementAt(index) {
        let curNode = this.head;
        if (index >= 0 && index <= this.count) {
            for (let i = 0; i < index; i++) {
                curNode = curNode.next;
            }
            return curNode;
        }
        return undefined;
    }
    insert(element, index) {
        const node = new Node(element);
        if (index >= 0 && index <= this.count) {
            if (index === 0) {
                node.next = this.head;
                this.head = node;
            } else {
                const previous = this.getElementAt(index - 1);
                const curNode = previous.next;
                node.next = curNode;
                previous.next = node;
            }
            this.count++;
            return true;
        }
        return false;
    }
    indexOf(element) {
        let curNode = this.head;
        for (let i = 0; i < this.count && curNode !== null; i++) {
            if (this.equalsFn(element, curNode.element)) {
                return i;
            }
            curNode = curNode.next;
        }
        return -1;
    }
    remove(element) {
        const index = this.indexOf(element);
        return this.removeAt(index);
    }
    size() {
        return this.count;
    }
    isEmpty() {
        return this.size() === 0
    }
    getHead() {
        return this.head;
    }
    toString() {
        let curNode = this.head.next;
        let objStr;
        if (this.head === null) {
            return '';
        }
        objStr = `${this.head.element}`
        for (let i = 0; i < this.size() && curNode !== null; i++) {
            objStr = `${objStr},${curNode.element}`;
            curNode = curNode.next;
        }
        return objStr;
    }
}

let link = new LinkedList();
link.push(1);
link.push(2);
console.log(link.removeAt(0));